import re, math;

'''
Python Base
'''
__author__ = 'HymanHu';

# ==== 输入 & 输出 ====
print("Hello World!");
# name = input("Please input: ");
# print(name);

# ==== 数据类型 ====
a = 12;
print("a的类型为 %s，值为: %s"%(type(a), a));
print(isinstance(a, int));
a = 43241.43214;
print("a的类型为 %s，值为: %s"%(type(a), a));
a = 3 > 2;
print("a的类型为 %s，值为: %s"%(type(a), a));
a = 4 + 5j;
print("a的类型为 %s，值为: %s"%(type(a), a));
a = r'cdcdascdas';
print("a的类型为 %s，值为: %s"%(type(a), a));
a = ["cdsa", 4321, True, None];
print("a的类型为 %s，值为: %s"%(type(a), a));
a = ("cdsa", 4321, True, None);
print("a的类型为 %s，值为: %s"%(type(a), a));
a = {"name":"hujiang","age":18};
print("a的类型为 %s，值为: %s"%(type(a), a));
a = set(["cdsa", 4321, True, None]);
print("a的类型为 %s，值为: %s"%(type(a), a));
a = None;
print("a的类型为 %s，值为: %s"%(type(a), a));

# ==== 运算符 ====
print((3 + 6 / 2) * 5 - 2);
print( 5 // 3)
print( 5 % 3)
print(3 ** 3)
print(True & False)
print(True | False)
print(not True)

# ==== String ====
print(ord('d'));
print(chr(99));
print('cdascdas'.encode("ascii"));
print('符浩天'.encode("utf-8"));
print(b'cdascdas'.decode('ascii'));
print(b'\xe8\x83\xa1\xe6\xb1\x9f'.decode('utf-8'));
print('DvfdDvfds'.lower() == 'DvfdDvfds');
print(len('cdsac'));
print(len('符浩天'));
print(len('cdsac'.encode('ascii')));
print(len('符浩天'.encode('utf-8')));
a = 'cdashujiangvfcdsachujiangdasdcdsa';
print(a.replace('hujiang', 'HYMAN'));
print(a);
print('sdfscdsavcdsa'.find('a'));
print('sdfscdsavcdsa'.rfind('a'));
print("姓名：%s，年龄：%02d，成绩：%.2f, 比例：%.2f%%"%('Hyman', 18, 90.4599, 34.5));
url = 'http://127.0.0.1/users/%s';
print(list(url % item for item in range(1, 10)));
print('姓名：{0}，年龄：{1:02d}，成绩：{2:.2f}, 比例：{3:.2f}%'.format('Hyman', 18, 90.4599, 34.5));
print('-'.join(["cdsa", 'cdsacdsac', 'cdsacdsac']));

# ==== 正则表达式 ====
ma = re.match('^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$', '1578579455@163.com');
if ma:
    print("匹配成功")
else:
    print("失败");
ma = re.match('a', 'cdadcas');
print(ma);
se = re.search('a', 'cdadcas');
print(se);
print("a b  c".split(" "));
print(re.split(r'\s+', 'a b  c'));
temp = "截至8月20日0时，全省累计报告新型冠状病毒肺炎确诊病例627例(其中境外输入86例），" \
           "累计治愈出院609例，死亡3例，目前在院隔离治疗15例，551人尚在接受医学观察。 ";
pattern = "截至8月20日0时，全省累计报告新型冠状病毒肺炎确诊病例(\d+)例\(其中境外输入(\d+)例\），" \
           "累计治愈出院(\d+)例，死亡(\d+)例，目前在院隔离治疗(\d+)例，(\d+)人尚在接受医学观察。 ";
se = re.search(pattern, temp);
print(se.groups());
print(se.group(0));
print(se.group(1));
print(se.group(2));

# ==== list ====
l = ["cdsacda", 23, 34.54, True, None];
print(l);
l = list(range(1, 10));
print(l);
print(l[-1]);
print(len(l));
l.insert(1, 'fuhaotian');
print(l);
# l.append(list(range(3)));
l += list(range(1, 3));
print(l);
l.remove(1);
print(l)
l.pop(0);
print(l);
l.pop();
print(l);

# ==== tuple ====
l = ["hj", "hyman"];
t = ("cdsacda", 23, 34.54, True, None, ["hj", "hyman"]);
print(t);
t[5][0] = 1;
print(t);
print(len(t));

# ==== dict ====
d = {"name":"fuhaotian", "age":18, "sex":"man"};
print(d);
print(d.get("sex"));
d["age"] = 19;
print(d);
d.pop("sex");
print(d);
d2 = {"aaa":"cdsacdas", "bbb":"feeew"};
print(dict(d, **d2));

# ==== set ====
# l = ["fuhaotian", "fuhaotian", True, None, 32, 45.54];
# print(l);
s = set(["fuhaotian", "fuhaotian", True, None, 32, 45.54]);
print(s);
s.add(222);
print(s);
s.remove('fuhaotian');
print(s);
s.pop();
print(s);
s2 = set(["fuhaotian", 222]);
print(s & s2);
print(s | s2);

# ==== 判断语句 ====
age = 58;
if age < 20:
    print("青年");
elif age >= 20 and age < 50:
    print("中年");
else:
    print("老年");
print("老年" if age > 50 else "非老年");

# ==== 循环语句 ====
count = 0;
for i in range(1, 10):
    count += i;
print(count);
while count > 0:
    count -= 5;
    print(count);

# ==== 函数 ====
def function1(name):
    print(name);
# ---- 位置参数 ----
def fun1(a, b = 0):
    print(a + b);
# ---- 可变参数 ----
def fun2(*args):
    count = 0;
    for i in args:
        count += i;
    print(count);
# ---- 可变关键字参数 ----
def fun3(**args):
    print(args);
# ---- 命名关键字参数 ----
def fun4(name, *, city):
    print(name, city);
# ---- 返回值 ----
def fun5(i):
    return i ** 2;
def fun6(base, angle):
    x = base + math.sin(math.pi / angle);
    y = base + math.cos(math.pi / angle);
    return x, y;
# ---- 空函数 ----
def fun7():
    pass;
# ---- 递归函数 ----
def fun8(number):
    if number == 1:
        return number;
    return number * fun8(number - 1);

# ==== 异常 ====
def fun():
    a = 10
    try:
        a = a / 0;
    except ZeroDivisionError as e:
        print(e);
    finally:
        print("必须执行");
    return a;

# ==== 类 ====
class Person(object):
    def __init__(self, id, name, age, sex):
        self.id = id;
        self.name = name;
        self.age = age;
        self.sex = sex;

    def getId(self):
        return self.id;
    def setId(self, id):
        self.id = id;

if __name__ == "__main__":
    # fun1(10, 44);
    # fun2(1, 2, 3, 4, 5, 6, 7, 8, 9);
    # fun3(name='fuhaotian', age=22, sex = "man");
    # fun4 = fun4("fuhaotian", city="chengdu");
    # print(fun4);
    # fun5 = fun5(4);
    # print(fun5);
    # fun6 = fun6(8, 6);
    # print(fun6);
    # fun7();
    # print(fun8(10));
    # fun();
    person = Person(1, 'fuhaotian', 18, 'man');
    print(person);
    person.setId(2);
    print(person.getId());
